/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Shop;
import java.sql.Connection;
import java.io.Serializable;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author ThanhTam
 */
public class ListType implements Serializable{
    
    //<editor-fold defaultstate="collapsed" desc="Variables">
    private ArrayList<TypeProduct> listType ;
    private Connection conn;
    private Statement stm ;
    private PreparedStatement pstm;
    private ResultSet rs;
    private boolean stateList = true;
    
    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }
      // </editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Constructor">
    public ListType(Connection _conn){ 
        listType = new ArrayList<TypeProduct>();
        this.conn = _conn;
	}
    // </editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    public ArrayList<TypeProduct> getListType() throws SQLException {
        refreshList();
        return listType; 
    }
   
    public int getItemCount() throws SQLException{
        refreshList();
        return listType.size();
    }
    
    public void AddType(TypeProduct tp) throws SQLException{
        if(!this.getListType().contains(tp)){
            try{
                pstm = conn.prepareStatement(
                "INSERT INTO loaisanpham ('maloai','tenloai') VALUES (?,?)");
                pstm.setString(2,tp.getNameType());
                pstm.setInt(1, tp.getIdType());
                pstm.executeUpdate();
            }
            catch(SQLException ex){
                System.err.println("Can't add item of ListType in to Database by error : \n"+ ex.getMessage());
            }
           finally{                
                    stm.close();
                    rs.close();                
                    stateList = true;
                }
        }
    }
    
    public void DelType(TypeProduct tp) throws SQLException{
       if(listType.contains(tp)){
        try{
            if(listType.contains(tp))
            pstm = conn.prepareStatement(
            "DELETE FROM loaisanpham WHERE maloai=?");
            pstm.setInt(1, tp.getIdType());
            pstm.executeUpdate();
        }
        catch(SQLException ex){
            System.err.println("Can't delete item of ListType by error : \n"+ ex.getMessage());
        }
        finally{
                stm.close();
                rs.close();
                stateList = true;
            }
       }
    }
    
    public void refreshList() throws SQLException{
        if(stateList){
            try{            
                    stm = conn.createStatement();              
                    rs = stm.executeQuery("SELECT maloai,tenloai FROM loaisanpham");
                    while(rs.next()){
                        listType.add(new TypeProduct(rs.getInt(1),rs.getString(2)));
                    }
              }
                catch(Exception ex){
                    System.err.println("Get list type not success");
                }
            finally{
                    stm.close();
                    rs.close();         
                }
               stateList = false;
           }
        }
    // </editor-fold>
}


